#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 3e5 + 10;

int n,h[N],id[N],b[N];
ll ans;
struct BIT {
	int arr[N];
	void add(int x, int v) { for(; x <= n; x += x & -x) arr[x] += v; }
	int query(int x) { int res = 0; for(; x; x -= x & -x) res += arr[x]; return res; }
} T ;

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	n = in; rep(i, 1, n) h[i] = b[i] = in,id[i] = i;
	sort(id + 1,id + n + 1,[](int a,int b) { return h[a] > h[b]; });
	sort(b + 1,b + n + 1);
	rep(i, 1, n) {
		int j = i; while(j <= n && h[id[i]] == h[id[j]]) j++; j--;
		rep(k, i, j) {
			int x = id[k];
			int res = T.query(x);
			ans += min(res,i - 1 - res);
		}
		rep(k, i, j) {
			int x = id[k]; T.add(x,1);
		}
		i = j;
	}
	cout << ans << endl; return 0;
}
